/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farmcode.plugin.objectinfo.jdbc.plugin;

import com.farmcode.api.plugin.impl.DefaultObjectInfoPlugin;
import com.farmcode.api.runtime.FARMCodeContext;
import com.farmcode.api.runtime.metadata.ObjectInfo;
import com.farmcode.plugin.objectinfo.jdbc.DatabaseInfo;
import com.farmcode.plugin.objectinfo.jdbc.ViewsDatabaseObjectInfo;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author oscar
 */
public class ObjectInfoJDBCViewPlugin extends DefaultObjectInfoPlugin{
    private DatabaseInfo databaseInfo;

    @Override
    public List<ObjectInfo> getObjectsInfo(FARMCodeContext context, String objectPattern) {
         List<ObjectInfo> objects = null;
        try {
            databaseInfo = new ViewsDatabaseObjectInfo(context);
            objects =databaseInfo.explore();
        } catch (SQLException ex) {
            Logger.getLogger(ObjectInfoJDBCViewPlugin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ObjectInfoJDBCViewPlugin.class.getName()).log(Level.SEVERE, null, ex);
        }
        return objects;
    }

    @Override
    public ObjectInfo getObjectAttributes(FARMCodeContext context, ObjectInfo objectInfo) {
        try {
            databaseInfo = new ViewsDatabaseObjectInfo(context);
            databaseInfo.exploreChilds(objectInfo);
        } catch (SQLException ex) {
            Logger.getLogger(ObjectInfoJDBCViewPlugin.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ObjectInfoJDBCViewPlugin.class.getName()).log(Level.SEVERE, null, ex);
        }
        return objectInfo;
    }

    @Override
    public String ping(FARMCodeContext context) {
        try {
            databaseInfo = new ViewsDatabaseObjectInfo(context);
            if(databaseInfo.test()){
                return "Connected to the server.";
            }else {
                return "Could not connect to server.";
            }
        } catch (SQLException ex) {
            Logger.getLogger(ObjectInfoJDBCViewPlugin.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ObjectInfoJDBCViewPlugin.class.getName()).log(Level.SEVERE, null, ex);
            return ex.getMessage();
        }
    }
}
